/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.events.graduation;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import pt.iasd.youthshelter.datamodel.Pathfinder;
import pt.iasd.youthshelter.wizards.core.WizardKeys;

public class EditGraduationSchoolWizardPanel2 implements WizardDescriptor.Panel<WizardDescriptor>, PropertyChangeListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private GraduationSchoolVisualPanel2 component;
    
    private WizardDescriptor descriptor;
    
    private final EventListenerList listeners = new EventListenerList();
    
    private List<Pathfinder> assigned;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public GraduationSchoolVisualPanel2 getComponent() {
        if (component == null) {
            component = new GraduationSchoolVisualPanel2(assigned);
        }
        component.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx("help.key.here");
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return true;
        // If it depends on some condition (form filled out...) and
        // this condition changes (last form field filled in...) then
        // use ChangeSupport to implement add/removeChangeListener below.
        // WizardDescriptor.ERROR/WARNING/INFORMATION_MESSAGE will also be useful.
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }

    @Override
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }

    @Override
    public void readSettings(WizardDescriptor wiz) {
        this.descriptor = wiz;
        assigned = (List<Pathfinder>)wiz.getProperty(WizardKeys.PROP_PATHFINDER_LIST);
    }

    @Override
    public void storeSettings(WizardDescriptor wiz) {
        /*TODO If wizard gets NullPointerException while adding a gaduation school
         * without any associated pathfinders change code to return empty list
         * instead of null. */
        if(assigned != null && !assigned.isEmpty()) {
            wiz.putProperty(WizardKeys.PROP_PATHFINDER_LIST, assigned);
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if(assigned == null || assigned.isEmpty()) {
            descriptor.getNotificationLineSupport().clearMessages();
            descriptor.getNotificationLineSupport().setWarningMessage(
                    bundle.getString("GraduationSchool.NoPathfinders.Error"));
        } else {
            descriptor.getNotificationLineSupport().clearMessages();
        }
        ChangeEvent event = new ChangeEvent(this);
        for(ChangeListener listener : listeners.getListeners(ChangeListener.class)) {
            listener.stateChanged(event);
        }
    }
}
